package com.xiaolanshu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiaolanshu.entity.Goods;
import com.xiaolanshu.entity.ShoppingCart;
import com.xiaolanshu.exception.BusinessException;
import com.xiaolanshu.mapper.ShoppingCartMapper;
import com.xiaolanshu.service.ShoppingCartService;
import com.xiaolanshu.vo.ErrorResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    //查询所有购物车商品
    @Override
    public List<ShoppingCart> shoppingCartInfo(Long userId) {
        //构造查询条件
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getDeleted,0).eq(ShoppingCart::getUserId,userId);
        return shoppingCartMapper.selectList(lqw);
    }
    //根据用户id和商品id删除购物程商品
    @Override
    public int removeGoods(Long userId, Long goodsId) {
        //创建shoppingCart对象，添加更新信息
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(userId);
        shoppingCart.setGoodsId(goodsId);
        shoppingCart.setDeleted(1);
        //构造更新条件,根据用户id和商品id设置deleted为1
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getGoodsId,goodsId).eq(ShoppingCart::getUserId,userId);
        return shoppingCartMapper.update(shoppingCart,lqw);

    }

    //保存商品信息到购物车
    @Override
    public void saveGoods(Long userId, Goods goods, Integer count) {

        //创建一个ShoppingCart对象保存要添加到购物车的信息
        ShoppingCart shoppingCart = new ShoppingCart();

        shoppingCart.setUserId(userId);
        shoppingCart.setGoodsId(goods.getId());
        shoppingCart.setGoodsName(goods.getGoodsName());
        shoppingCart.setPrice(goods.getPrice());
        shoppingCart.setPicture(goods.getPicture());
        shoppingCart.setCount(count);
        //商品金额为商品数量*商品价格
        shoppingCart.setAmount(count*goods.getPrice());

        shoppingCartMapper.insert(shoppingCart);

    }
    //根据用户id和商品id增加商品数量
    @Override
    public int addGoodsCount(Long userId, Long goodsId) {

        //根据商品id和用户查询购物车商品信息,
        LambdaQueryWrapper<ShoppingCart> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(ShoppingCart::getGoodsId,goodsId).eq(ShoppingCart::getUserId,userId);
        ShoppingCart shoppingCart = shoppingCartMapper.selectOne(lqw1);
        if (shoppingCart == null){
            //抛出自定义业务异常
            throw new BusinessException(ErrorResult.goodsError());
        }

        //将购物车商品数量加1
        shoppingCart.setCount(shoppingCart.getCount()+1);
        //商品金额随着改变
        shoppingCart.setAmount(shoppingCart.getAmount()+shoppingCart.getPrice());
        //更新购物车信息
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getGoodsId,goodsId).eq(ShoppingCart::getUserId,userId);
        return shoppingCartMapper.update(shoppingCart,lqw);

    }
    //根据用户id和商品id减少商品数量
    @Override
    public void reduceGoodsCount(Long userId, Long goodsId) {
        //根据商品id和用户查询购物车商品信息,
        LambdaQueryWrapper<ShoppingCart> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(ShoppingCart::getGoodsId,goodsId).eq(ShoppingCart::getUserId,userId);
        ShoppingCart shoppingCart = shoppingCartMapper.selectOne(lqw1);
        if (shoppingCart == null){
            //抛出自定义业务异常
            throw new BusinessException(ErrorResult.goodsError());
        }
        //如果商品数量减少到0,则移除该购物车商品
        if (shoppingCart.getCount()-1 == 0){
            //自调用上面定义的删除方法
           this.removeGoods(userId,goodsId);
        }
        else {
            //将购物车商品数量减1
            shoppingCart.setCount(shoppingCart.getCount()-1);
            //商品金额随着改变
            shoppingCart.setAmount(shoppingCart.getAmount()-shoppingCart.getPrice());
            //更新购物车信息
            LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
            lqw.eq(ShoppingCart::getGoodsId,goodsId).eq(ShoppingCart::getUserId,userId);
            shoppingCartMapper.update(shoppingCart,lqw);
        }

    }

    //根据用户id和商品id查询购物车中的商品信息
    @Override
    public ShoppingCart findGoods(Long userId, Long goodsId) {
        //构造查询条件
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userId).eq(ShoppingCart::getGoodsId,goodsId).eq(ShoppingCart::getDeleted,0);
        return shoppingCartMapper.selectOne(lqw);
    }
    //从购物车中删除商品
    @Override
    public void delete(Long goodsId,Long userId) {

        //构造删除条件
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getGoodsId,goodsId).eq(ShoppingCart::getUserId,userId);
        //将deleted改为1表删除
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setDeleted(1);

        shoppingCartMapper.update(shoppingCart,lqw);

    }


}
